package day02;

import com.blackhorse.domain.QueryVo;
import com.blackhorse.domain.User;
import com.blackhorse.xml.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Demo2_DynamicSQL {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    private UserMapper mapper;

    @Before
    public void init() {
        try {
            InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(in);
            sqlSession = sqlSessionFactory.openSession();
            mapper = sqlSession.getMapper(UserMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void destory() {
        sqlSession.commit();
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testFindUserByCondition() {
        User uservo = new User();
        uservo.setUsername("张三");
        uservo.setSex("男");
        List<User> users = mapper.findUserByCondition(uservo);
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void testFindUserInIds() {
        QueryVo queryVo = new QueryVo();
        ArrayList ids = new ArrayList();
        ids.add(1);
        ids.add(2);
        ids.add(4);
        queryVo.setIds(ids);

        List<User> users = mapper.findUserInIds(queryVo);
        for (User user : users) {
            System.out.println(user);
        }
    }
}
